/*
 * IndexNode.cpp
 *
 *  Created on: Oct 9, 2011
 *      Author: USHonDan
 */

#include "IndexNode.h"
#include "LeafNode.h"

IndexNode::IndexNode(Node *nodePtr) : Node(INDEX_NODE), _child(nodePtr) {
}

IndexNode::~IndexNode() {
}

//Return pointer to [idx]th child node in this node's nodegroup
Node *IndexNode::GetChildNode(int idx) {
	if(idx <= NumberOfKeys()) { //more stringent than idx <= d
		if(_child->GetNodeType() == LEAF_NODE)
			return &(((LeafNode *)_child)[idx]); //assumes nodegroup correctly preallocated by index manager
		else
			return &(((IndexNode *)_child)[idx]);
	}

	return NULL;
}

//Return child node that gets one step closer to input search key
Node *IndexNode::SearchKeyNextChild(int key) {
	return GetChildNode(SearchKey(key));
}

//Split this index node into input empty index node by copying keys and setting correct nodegroup pointer
int IndexNode::SplitIndexNode(IndexNode *nextEmpty, int splitIdx) {
	for(int idx = splitIdx; idx < NumberOfKeys(); idx++)
		nextEmpty->InsertKey(_keys[idx]);

	SetNumberOfKeys(splitIdx-1); //remove key that is being passed up to parent node, 2011-10-14

	return _keys[splitIdx-1];
}
